package cn.dpg.study;

import java.util.concurrent.*;

public class DynamicThreadPoolDemo {


    enum ThreadPoolExecutorStatusEnum {
        ChangeBefore,//改变之前
        ChangeLater,//改变之后
        CreateTask;//创建任务
    }


    /**
     * 创建 自定义线程池
     * 核心线程数为 3
     * 最大线程数为 5
     * 休眠回收时间为 30s
     * 阻塞队列长度为 10
     *
     * @return
     */
    private static ThreadPoolExecutor createThreadPool() {
        return new ThreadPoolExecutor(5,
                5,
                30,
                TimeUnit.SECONDS,
                new MyLinkedBlockingQueue<>(10)
        );
    }

    /**
     * 打印线程池状态信息
     */
    private static void printExecutorInfo(ThreadPoolExecutor executor, ThreadPoolExecutorStatusEnum statusEnum) {
        System.out.println(Thread.currentThread().getName() + "-" + statusEnum + ":" +
                "核心线程数-corePoolSize:" + executor.getCorePoolSize() +
                "活动线程数-activeCount:" + executor.getActiveCount() +
                "最大线程数-maximumPoolSize:" + executor.getMaximumPoolSize() +
                "线程池活跃度-threadPoolUsePercentage:" + String.format("%.2f", executor.getActiveCount() * 100.0 / executor.getMaximumPoolSize()) +
                "完成任务数-completedTaskCount:" + executor.getCompletedTaskCount() +
                "当前队列排队数-queueSize:" + executor.getQueue().size() +
                "当前队列剩余数-queueRemainingCapacity:" + executor.getQueue().remainingCapacity() +
                "当前队列使用度:queueUsePercentage" + String.format("%.2f", executor.getQueue().size() * 100.0 / (executor.getQueue().size() + executor.getQueue().remainingCapacity())));

    }

    public static void main(String[] args) throws InterruptedException {
        long timeStart = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(15);
        ThreadPoolExecutor executor = createThreadPool();
        //允许核心线程被回收
        executor.allowCoreThreadTimeOut(true);
        for (int i = 0; i < 15; i++) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    printExecutorInfo(executor, ThreadPoolExecutorStatusEnum.CreateTask);
                    try {
                        //休眠 10 s
                        Thread.sleep(10 * 1000);
                        latch.countDown();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        printExecutorInfo(executor, ThreadPoolExecutorStatusEnum.ChangeBefore);
        //动态改变线程池参数
        executor.setCorePoolSize(10);
        executor.setMaximumPoolSize(10);
        MyLinkedBlockingQueue queue = (MyLinkedBlockingQueue) executor.getQueue();
        queue.setCapacity(30);
        printExecutorInfo(executor, ThreadPoolExecutorStatusEnum.ChangeLater);
        //等待所有任务执行完
        latch.await();
        long timeEnd = System.currentTimeMillis();
        System.out.println("耗费时间:" + (timeEnd - timeStart) / 1000);
    }

}
